package com.heima.article.listener;

import com.heima.article.msg.ArticleBehaviorStreamMsg;
import com.heima.article.service.ApHotArticlesService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 监听行为输出流的linstener
 * @author Fuqiang
 * @since :2023-04-03
 */
@Slf4j
@Component
public class BehaviorStreamOutputListener {

    @Autowired
    ApHotArticlesService hotArticlesService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_SCORE_CONSUMER_TOPIC)
    public void onMessages(ConsumerRecord<String, String> record) {
        if (record == null) {
            return;
        }
        log.info("收到了行为数据流处理的消息: key: {},value: {}", record.key(), record.value());
        ArticleBehaviorStreamMsg streamMsg = JsonUtils.toBean(record.value(), ArticleBehaviorStreamMsg.class);
        // 更新文章的分值
        hotArticlesService.updateArticleScore(streamMsg);
    }
}
